package com.kingint.web.admin.base.controller;


import com.kingint.mbg.group.Insert;
import com.kingint.mbg.group.Update;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.kingint.common.unitedresult.K;

import com.kingint.web.admin.base.service.BasGoodsService;
import com.kingint.mbg.bas.entity.BasGoods;


/**
 * @描述：消耗品和布草/易碎品相关表控制类
 * @作者: wxh
 * @日期: 2020-12-22
 */
@RestController
@RequestMapping("/basGoods")
@Api(value = "BasGoodsController", description = "消耗品和布草/易碎品相关表")
@CrossOrigin
public class BasGoodsController {

    @Autowired
    public BasGoodsService basGoodsService;


    @ApiOperation(value = "添加消耗品和布草/易碎品相关表")
    @PostMapping("/create")
    //  @PreAuthorize("hasAuthority('basGoods:create')")
    public Object create(@Validated(Insert.class) @RequestBody BasGoods basGoods) {
        int i = basGoodsService.insertGoodsInfo(basGoods);
        if (i > 0) {
            return K.ok().data(basGoods.getId());
        } else {
            return K.error();
        }
    }


    @ApiOperation(value = "更新消耗品和布草/易碎品相关表")
    @PutMapping("/update")
    // @PreAuthorize("hasAuthority('basGoods:update')")
    public Object update(@Validated(Update.class) @RequestBody BasGoods basGoods) {
        int i = basGoodsService.updateGoodsInfo(basGoods);
        if (i > 0) {
            return K.ok().data(basGoods.getId());
        } else {
            return K.error().message("删除失败，还有物品或者分类绑定到它");
        }
    }


    @ApiOperation(value = "删除消耗品和布草/易碎品相关表")
    @DeleteMapping("/delete/{id}")
    // @PreAuthorize("hasAuthority('basGoods:delete')")
    public Object delete(@PathVariable("id") Integer id) {
        int i = basGoodsService.deleteGoodsInfo(id);
        if (i > 0) {
            return K.ok();
        } else {
            return K.error();
        }
    }

    @ApiOperation(value = "根据消耗品和布草/易碎品相关表名称分页获取消耗品和布草/易碎品相关表列表")
    @GetMapping("/list")
    // @PreAuthorize("hasAuthority('basGoods:list')")
    public Object getList(BasGoods basGoods,
                          @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                          @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {

        return    basGoodsService.getGoodList(basGoods, pageNum, pageSize);
    }

    @ApiOperation(value = "根据编号查询消耗品和布草/易碎品相关表信息")
    @GetMapping("/{id}")
    //  @PreAuthorize("hasAuthority('basGoods:get')")
    public Object getItem(@PathVariable("id") Long id) {
        BasGoods basGoods = basGoodsService.getById(id);
        return K.ok().data(basGoods);
    }
}